def print_file_info(file_name):
    file = None;
    try:
        file = open(file_name, 'r', encoding='UTF-8')
    except FileNotFoundError as e:
        print(e)
        print(f"文件{file_name}不存在")
    else:
        count = 1
        for content in file:
            content = content.strip()
            print(f"第{count}行内容为：{content}")
            count += 1
    finally:
        # 当发生异常时，file的值为初始值：None,而None代表false
        if file:
            file.close()


def append_to_file(file_name, content):
    try:
        append_file = open(file_name, 'a', encoding="UTF-8")
    except Exception as e:
        print(e)
        print(f"向文件{file_name}中追加内容失败，待追加的内容为：{content}")
    else:
        append_file.write(content)
        append_file.flush()
    finally:
        append_file.close()


if __name__ == "__main__":
    print_file_info("D:\\good.txt")
    append_to_file("D:\\good.txt", "追加一行\n")
    append_to_file("D:\\good.txt", "追加第二行\n")
